//index.js
//获取应用实例

const app = getApp()

Page({
  data: {
    map: false,
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    scale: 14,
    latitude: 0,
    longitude: 0,
    beijingheight:0,
    markers: [{
        iconPath: "../../pages/image/zhong.png",
        // title: "青岛北站",
      id: 0, 
      latitude: 36.171174,
      longitude: 120.377583,
      width: 25,
      height: 30,
      
        label: {
          content: '青岛北站',
          fontSize: 14,
          color: '#00000',

          textAlign: "center",

        }
      }, {
        // title: "创智谷",
        iconPath: "../../pages/image/qi.png",
        id: 6,
        latitude: 36.150380,
        longitude: 120.485620,
        width: 25,
        height: 30,
        label: {
          content: '创智谷',
          fontSize: 14,
          color: '#000000',

          textAlign: "center"
        }
      },
      {
        iconPath: "../../pages/image/bus.png",
        anchor: { x: 0.5, y: 0.5 },
        id: 99,
        latitude: 0,
        longitude: 0,
        width: 30,
        height: 30,

      }, {
        iconPath: "../../pages/image/dian.png",
        id: 1,
        // title: "鞍山二路北站", 
         latitude: 36.179926,
      longitude: 120.381540,
        width: 15,
        height: 15,
        anchor: { x: 0.5, y: 0.5 },
        anchorX:15,
          anchorY:0,
        label: {
          content: '沧安路',
          fontSize: 14,
          color: '#000000',

          textAlign: "center"
        }
      }, {

        iconPath: "../../pages/image/dian.png",
        id: 2, 
        anchor: { x: 0.5, y: 0.5 },
        // title: "金水路下王埠站",
        latitude: 36.176526,
        longitude: 120.425144,
        width: 15,
        height: 15,
        label: {
          content: '金水路下王埠站',
          fontSize: 14,
          color: '#000000',

          textAlign: "center"
        }
      }, {

        iconPath: "../../pages/image/dian.png",
        id: 3, 
        anchor: { x: 0.5, y: 0.5 },
        // title: "李村公园",
        latitude: 36.166034,
        longitude: 120.426174,
        width: 15,
        height: 15,
        label: {
          content: '李村公园',
          fontSize: 14,
          color: '#000000',

          textAlign: "center"
        }
      }, {

        iconPath: "../../pages/image/dian.png",
        id: 4, 
        anchor: { x: 0.5, y: 0.5 },
        // title: "合川路",
        // label: "合川路",
        latitude: 36.167249,
        longitude: 120.457631,
        width: 15,
        height: 15,
        label: {
          content: '合川路',
          fontSize: 14,
          color: '#000000',

          textAlign: "left"
        }
      }, {

        iconPath: "../../pages/image/dian.png",
        id: 5, 
        anchor: { x: 0.5, y: 0.5 },
        // title: "宾川路南站",
        latitude: 36.170442,
        longitude: 120.477882,
        width: 15,
        height: 15,
        label: {
          content: '宾川路南站',
          fontSize: 14,
          color: '#000000',

          textAlign: "center"
        }
      } 
    ],

    polyline: [{
      points: [],
      color: "#4287ff",
      width: 5,
      dottedLine: false,
      arrowLine: true
    }]
    

  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  onLoad: function() {

    var that = this;
    var polyline = 'polyline[0].points'
    var buslongitude = 'markers[2].longitude'
    var buslatitude = 'markers[2].latitude'
    var latitude = that.data.latitude
    var longitude = that.data.longitude
    wx.getLocation({
      type:"gcj02",
      success: function(res) {
        that.setData({
          longitude:res.longitude,
          latitude:res.latitude
        })
      },
    })
    var beijingheight = that.data.beijingheight
    wx.getSystemInfo({
      success: function (res) {
        that.setData({

          beijingheight: res.windowHeight
        })
      },
    })
    wx.request({
      url: 'http://www.zyhgps.com/api/Tracking.aspx?sn=14143391966&mapType=google&key=OUCHUANG20170602A009',
      success: function(res) {
        console.log(res.data)
        that.setData({
          [buslongitude]: res.data.lng,
          [buslatitude]: res.data.lat
        })
      }
    })
 
    

    wx.request({
      url: 'https://restapi.amap.com/v3/direction/driving?',
      data: {

        origin: "120.377583,36.171174",

        destination: "120.485620,36.150380",
        key: "96b344c49b94b68333551a83d93e784c",
        waypoints: "120.377583,36.171174;120.381540,36.179926;120.425144,36.176526;120.426174,36.166034;120.457631,36.167249;120.477882,36.170442;120.485620,36.150380"
      },

      success: function(res) {
        // console.log(res.data.route.paths[0].steps)
        var step = res.data.route.paths[0].steps
        var arr1 = [];
        for (var i = 0; i < step.length; i++) {
          var str = step[i].polyline
          var arr = str.split(";");
          var arra = Array.from(arr)
          // {latitude: 0, longitude: 0}
          // console.log(arra)
          for (var j = 0; j < arra.length; j++) {
            var arraa = arra[j]
            var result = arraa.split(",");
            var json = {
              longitude: Number(result[0]),
              latitude: Number(result[1])

            }
            // console.log(json)
            // console.log(json)
            arr1.push(json)
          }

          // arr1.push(json)

        }
        console.log(arr1)



        that.setData({

          [polyline]: arr1
        })

      }
    })
  },
  refresh:function(){
    var that = this;
    var buslongitude = 'markers[2].longitude'
    var buslatitude = 'markers[2].latitude'
    wx.request({
      url: 'http://www.zyhgps.com/api/Tracking.aspx?sn=14143391966&mapType=google&key=OUCHUANG20170602A009',
      success: function (res) {
        console.log(res.data)
        that.setData({
          [buslongitude]: res.data.lng,
          [buslatitude]: res.data.lat
        })
      }
    })
  },
  getUserInfo: function() {
    var that = this
    var code = that.data.code
    var encryptedData = that.data.encryptedData
    var iv = that.data.iv
    wx.login({
      success: function(res) {
        var service_url = 'https://www.blockchainqkl.cn/cms/user/bingxuexiehuifive/login.do?code=' + res.code; //需要将服务器域名添加到小程序的request合法域名中，而且必须是https开头  
        wx.request({
          url: service_url,
          data: {},
          method: 'GET',
          success: function(res) {
            console.log(res.data[0]);
            wx.showToast({
              title: '获取openid成功',
              icon: 'success',
              image: '',
              duration: 1500,
              mask: true,
              success: function(res) {},
              fail: function(res) {},
              complete: function(res) {},
            })
            if (res.data[0] != null && res.data[0] != undefined && res.data[0] != '') {
              wx.setStorageSync("openid", res.data[0].openid); //将获取的openid存到缓存中  
            }
          }
        });
      }
    });

    // wx.login({
    //   success: function (res) {


    //     code = res.code;//发送给服务器的code 





    //         wx.request({

    //           url: 'https://www.blockchainqkl.cn/cms/user/bingxuexiehuifive/login.do?code=' +code,
    //           data: {
    //             code: code,

    //           },
    //           header: {
    //             'content-type': 'application/json'
    //           },
    //           method: "GET",
    //           success: function (result) {
    //             console.log(result)
    //             if (result.data != null && result.data != undefined && result.data != '') {
    //               wx.setStorageSync("openid", result.data.openid);//将获取的openid存到缓存中  
    //             }
    //           }
    //         })

    //       }
    //       ,
    //       fail: function () {
    //         console.log("大一")
    //       }


    // })





  },
  wxpay: function() {
    var doWxPay = require('../../utils/wxpay.js')
    var that = this;
    var service_url = 'https://www.blockchainqkl.cn/cms/user/bingxuexiehuifive/wxPay.do'
    var value = wx.getStorageSync('openid');

    wx.request({
      url: service_url,

      data: {
        openid: value
      },
      method: 'GET',
      success: function(res) {
        console.log(res.data[0].data);
        doWxPay.doWxPay(res.data[0])
      },
      fail: function() {
        wx.showToast({
          title: '获取失败',
          icon: 'success',
          image: '',
          duration: 1500,
          mask: true,
          success: function(res) {},
          fail: function(res) {},
          complete: function(res) {},
        })
      }
    });
  },
  onShareAppMessage: function() {

  },
  ditu:function(){
    wx.navigateTo({
      url: '../../pages/pay/pay',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  }
})